package com.serena.da.rest.data_providers.xlsx;

import com.serena.da.rest.services.deploy.ComponentService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Spitkovskyi Igor on 8/26/2014.
 */
public class ComponentXLSXDataProvider extends XLSXDataProvider {

    @DataProvider
    public static Iterator<Object[]> createComponentProcessDataProvider (ITestContext context) throws Exception{
        String worksheetName = "CreateComponentProcess";
        int NumberOfParameters = 5;
        final String ComponentNameHeader = "component_name";
        final String ProcessNameHeader = "process_name";
        final String ProcessDescriptionHeader = "process_description";
        final String ProcessTypeHeader = "process_type";
        final String ProcessRoleHeader = "component_role";

        int ComponentNameColumnIndex = -1;
        int ProcessNameColumnIndex = -1;
        int ProcessDescrColumnIndex = -1;
        int ProcessTypeColumnIndex = -1;
        int ProcessRoleColumnIndex = -1;

        //Container for data, matching format of TestNG data providers.
        List<Object[]>  dataToBeReturned = new ArrayList<Object[]>();
        XSSFSheet dataSheet = getDataWorksheet(context, worksheetName);

        if(dataSheet == null){
            throw new Exception ("No data sheet was found.");
        }

        //Identify indexes of columns which contain values for corresponding required parameter
        Row headersRow = dataSheet.getRow(dataSheet.getFirstRowNum());
        Iterator<Cell> headerCells = headersRow.cellIterator();
        while(headerCells.hasNext()){
            Cell headerCell = headerCells.next();
            String cellValue = headerCell.getStringCellValue();
            if(cellValue.equalsIgnoreCase(ComponentNameHeader)){
                ComponentNameColumnIndex = headerCell.getColumnIndex();
            }else if(cellValue.equalsIgnoreCase(ProcessNameHeader)){
                ProcessNameColumnIndex = headerCell.getColumnIndex();
            }else if(cellValue.equalsIgnoreCase(ProcessDescriptionHeader)){
                ProcessDescrColumnIndex = headerCell.getColumnIndex();
            }else if(cellValue.equalsIgnoreCase(ProcessTypeHeader)){
                ProcessTypeColumnIndex = headerCell.getColumnIndex();
            }else if(cellValue.equalsIgnoreCase(ProcessRoleHeader)){
                ProcessRoleColumnIndex = headerCell.getColumnIndex();
            }
        }

        if(ComponentNameColumnIndex < 0 || ProcessNameColumnIndex < 0 || ProcessDescrColumnIndex < 0 ||
                ProcessTypeColumnIndex <0 ||  ProcessRoleColumnIndex < 0){
            throw new Exception ("Could not identify all the columns which contain values for required parameters.");
        }

        //Create temporary container for data values parsed from each row in worksheet.
        String [] parameters = new String[NumberOfParameters];

        //Iterate through rows which contain data.
        Iterator<Row> rowsIterator = dataSheet.rowIterator();
        boolean headerRowSkipped = false;
        while(rowsIterator.hasNext()){
            Row row = rowsIterator.next();
            //Skip top row, which contains headers.
            if(!headerRowSkipped){
                headerRowSkipped = true;
                continue;
            }

            parameters[0] = row.getCell(ComponentNameColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();
            parameters[1] = row.getCell(ProcessNameColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();
            parameters[2] = row.getCell(ProcessDescrColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();
            parameters[3] = row.getCell(ProcessTypeColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();
            parameters[4] = row.getCell(ProcessRoleColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();

            dataToBeReturned.add(new Object[] {parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]});
        }

        return dataToBeReturned.iterator();
    }

    @DataProvider
    public static Iterator<Object[]> createComponentProcessWorkflowDataProvider (ITestContext context) throws Exception{
        String worksheetName = "CreateComponentProcessWorkflow";
        int NumberOfParameters = 5;
        final String ComponentNameHeader = "component_name";
        final String ProcessNameHeader = "process_name";
        final String WorkflowStepNamesHeader = "step_names";
        final String PluginNamesHeader = "plugin_names";
        final String CommandNamesHeader = "command_names";

        int ComponentNameColumnIndex = -1;
        int ProcessNameColumnIndex = -1;
        int WorkflowStepNamesColumnIndex = -1;
        int PluginNamesColumnIndex = -1;
        int CommandNamesColumnIndex = -1;

        //Container for data, matching format of TestNG data providers.
        List<Object[]>  dataToBeReturned = new ArrayList<Object[]>();
        XSSFSheet dataSheet = getDataWorksheet(context, worksheetName);

        if(dataSheet == null){
            throw new Exception ("No data sheet was found.");
        }

        //Identify indexes of columns which contain values for corresponding required parameter
        Row headersRow = dataSheet.getRow(dataSheet.getFirstRowNum());
        Iterator<Cell> headerCells = headersRow.cellIterator();
        while(headerCells.hasNext()){
            Cell headerCell = headerCells.next();
            String cellValue = headerCell.getStringCellValue();
            if(cellValue.equalsIgnoreCase(ComponentNameHeader)){
                ComponentNameColumnIndex = headerCell.getColumnIndex();
            }else if(cellValue.equalsIgnoreCase(ProcessNameHeader)){
                ProcessNameColumnIndex = headerCell.getColumnIndex();
            }else if(cellValue.equalsIgnoreCase(WorkflowStepNamesHeader)){
                WorkflowStepNamesColumnIndex = headerCell.getColumnIndex();
            }else if(cellValue.equalsIgnoreCase(PluginNamesHeader)){
                PluginNamesColumnIndex = headerCell.getColumnIndex();
            }else if(cellValue.equalsIgnoreCase(CommandNamesHeader)){
                CommandNamesColumnIndex = headerCell.getColumnIndex();
            }
        }

        if(ComponentNameColumnIndex < 0 || ProcessNameColumnIndex < 0 || WorkflowStepNamesColumnIndex < 0 ||
                PluginNamesColumnIndex <0 ||  CommandNamesColumnIndex < 0){
            throw new Exception ("Could not identify all the columns which contain values for required parameters.");
        }

        //Create temporary container for data values parsed from each row in worksheet.
        String [] parameters = new String[NumberOfParameters];

        //Iterate through rows which contain data.
        Iterator<Row> rowsIterator = dataSheet.rowIterator();
        boolean headerRowSkipped = false;
        while(rowsIterator.hasNext()){
            Row row = rowsIterator.next();
            //Skip top row, which contains headers.
            if(!headerRowSkipped){
                headerRowSkipped = true;
                continue;
            }

            /*
            int numberOfColumns = row.getPhysicalNumberOfCells();

            if(numberOfColumns < 5){
                throw new Exception ("Number of columns with data is less than number of required parameters.");
            }
            */
            parameters[0] = row.getCell(ComponentNameColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();
            parameters[1] = row.getCell(ProcessNameColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();
            parameters[2] = row.getCell(WorkflowStepNamesColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();
            parameters[3] = row.getCell(PluginNamesColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();
            parameters[4] = row.getCell(CommandNamesColumnIndex, Row.CREATE_NULL_AS_BLANK).getStringCellValue();

            dataToBeReturned.add(new Object[] {parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]});
        }

        return dataToBeReturned.iterator();
    }
}
